import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.MyPrint;
import java.text.DecimalFormat;

import local.util.MyStringFormat;
import local.util.MyPrint;

/**********************************************************************************
 * class HttpTableDataLoader
 * public setUrl (String)
 * public String getUrl ()
 * public void connectToUrl ()
 * public String getUrlReturnString ()
 * public Vector stringToVector (String)
 * public void setStartString ( String )
 * public void setEndString ( String )
 * public Vector selectUsefulElements (Vector)
 * public void insertIntoDataBase( String query_string)
 **********************************************************************************/
public class HttpDataLoader {    

    private static boolean debug = false;
    private static boolean save = false;
    private String myUrl = null;
    private String urlReturnString = new String();
    private Vector urlReturnVector= new Vector(10);
    private String startString= null;
    private String endString  = null;

    public static void main (String[] args) {
	String url_return_string = null;
	Vector url_return_vector = new Vector ();
	HttpDataLoader mydl = new HttpDataLoader ();
	mydl.setUrl("http://www.mrci.com/qpnight.asp");
	mydl.connectToUrl();
	url_return_string = mydl.getUrlReturnString();
	//System.out.println(url_return_string);
	url_return_vector = mydl.stringToVector (url_return_string);

	mydl.setStartString("<BODY");
	mydl.setEndString("</BODY");
	url_return_vector = mydl.selectUsefulElements (url_return_vector);
	//url_return_vector = mydl.deselectElement (url_return_vector, "Industries");
	//url_return_vector = mydl.deselectElement (url_return_vector, "&#183");
	//url_return_vector = mydl.deselectElement (url_return_vector, ":");
	//MyPrint.printVector(url_return_vector,1);
	MyPrint.printVector(url_return_vector,12);

    }

    public void setUrl (String url) {

	myUrl = url;
    }

    public String getUrl () {

	return myUrl;
    }

    public void connectToUrl () {

	String url_return_string = "";

	URL url ;

	HttpsURLConnection httpsConnection = null;

	try {            
	    url = new URL (myUrl);

	    try {              

		StringBuffer buff1= new StringBuffer("");  
		if (myUrl.startsWith("https:")) {
		    httpsConnection = (HttpsURLConnection)url.openConnection();                
		    httpsConnection.setDoOutput(false);                
		    httpsConnection.connect();                

		    System.out.println("Connection Code and Message:" + 
				       httpsConnection.getResponseCode() + 
				       " - " + httpsConnection.getResponseMessage()+"\n");
		   

		    BufferedReader in = new BufferedReader(new InputStreamReader
			(httpsConnection.getInputStream()));

		    url_return_string = in.readLine();

		    while (url_return_string != null) {
			buff1.append(url_return_string);
			url_return_string = in.readLine();
		    }

		    in.close();            
		}
		else if (myUrl.startsWith("http:")) {   

		    HttpURLConnection httpConnection = null;
		    httpConnection = (HttpURLConnection)url.openConnection();                
		    httpConnection.setDoOutput(false);                
		    httpConnection.connect();                

		   
		    System.out.println("Connection Code and Message:" + 
				       httpConnection.getResponseCode() + 
				       " - " + httpConnection.getResponseMessage()+"\n");
		    if (httpConnection.getResponseCode()!=200) {
			return;
		    }
		    BufferedReader in = new BufferedReader(new InputStreamReader
			(httpConnection.getInputStream()));

		    url_return_string = in.readLine();
		    
		    while (url_return_string != null) {
			buff1.append(url_return_string);
			url_return_string = in.readLine();
		    }

		    in.close();            
		}

		url_return_string = buff1.toString();
		url_return_string = url_return_string.replaceAll("\n","");
		url_return_string = url_return_string.replaceAll("\t","");
		url_return_string = url_return_string.replaceAll("&nbsp;","");
		url_return_string = url_return_string.replaceAll("&amp;","");
		url_return_string = url_return_string.replaceAll("<NOBR>","");
		url_return_string = url_return_string.replaceAll("<small>","");
		url_return_string = url_return_string.replaceAll(">",">\n");
		url_return_string = url_return_string.replaceAll("<","\n<");
		url_return_string = url_return_string.replaceAll("</","\n</");
	

		urlReturnString = url_return_string;

	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	     System.out.println(mue.getMessage());            
	    System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }

    public String getUrlReturnString () {
	return urlReturnString;

    }

    public Vector stringToVector (String input) {

	Vector url_return_vector = new Vector ();
	String element = null;
	StringTokenizer tokens = new StringTokenizer(input, "\n");
	while (tokens.hasMoreTokens()) {
	    element = tokens.nextToken();
	    if (element != null) {
		url_return_vector.addElement(element);
	    }
	}

	return url_return_vector;
    }

    public void setStartString ( String start_str ) {
	startString = start_str;
    }

    public void setEndString ( String end_str ) {
	endString = end_str;
    }

    public Vector selectUsefulElements (Vector v_in) {

	Vector v_out = new Vector (10);
	String text = null;
	Enumeration penum = v_in.elements();
       
	while ( penum.hasMoreElements()) {

	    text = (String)penum.nextElement();
	    if (text.startsWith(startString)) {
		v_out.addElement(text);
		break;
	    }
	}
	
	while ( penum.hasMoreElements() ) {
	    text = (String)penum.nextElement();
	    if (text.startsWith(endString)) {
		break;
	    }
	   	    
	    if ( ! (text.startsWith("<") )) {
		v_out.addElement(text);
	    }
	}
	
	return v_out;
    }

    public Vector deselectElement (Vector v_in, String d_element) {

	Vector v_out = new Vector (10);
	String text = null;
	Enumeration penum = v_in.elements();
       
	while ( penum.hasMoreElements()) {

	    text = (String)penum.nextElement();

	    if (!text.startsWith(d_element)) {
		v_out.addElement(text);
	    }
	}
	return v_out;
    }

    public void insertIntoDataBase( String query_string) {

	DataBaseInsert myInsert = new DataBaseInsert();	
	
	myInsert.setQueryString(query_string);
	myInsert.insertData();

    }         
}








